package com.momo.elasticsearch;

import com.google.common.collect.Maps;
import com.momo.elasticsearch.entity.ElasticsearchDataType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created by MOMO on 2019/3/11.
 *
 */
@RestController
@Slf4j
public class ElasticsearchController {
    @Autowired
    private ElasticsearchUtil elasticsearchUtil;

    @PostMapping("/createIndex")
    public Object createIndex(@RequestBody CreateIndexReq createIndexReq) throws Exception {
        //  结构: {name:{type:text}} {age:{type:integer}}
        Map<String, Object> properties = Maps.newHashMap();
        properties.put("name", ElasticsearchDataType.getType(ElasticsearchDataType.INTEGER,null,null));
        properties.put("age", ElasticsearchDataType.getType(ElasticsearchDataType.INTEGER,null,null));
        properties.put("id", ElasticsearchDataType.getType(ElasticsearchDataType.TEXT,null,null));
        properties.put("birth_date", ElasticsearchDataType.getType(ElasticsearchDataType.DATE,null,null));
        return elasticsearchUtil.createIndex(createIndexReq, properties);
    }

    @PostMapping("/getIndex")
    public Object getIndex(@RequestBody IndexDeleteReq indexDeleteReq) throws Exception {
        return elasticsearchUtil.getIndex(indexDeleteReq);
    }

    @PostMapping("/putMapping")
    public Object putMapping(@RequestBody CreateIndexReq createIndexReq) throws Exception {
        //  结构: {name:{type:text}} {age:{type:integer}}
        Map<String, Object> properties = Maps.newHashMap();
        properties.put("name", ElasticsearchDataType.getType(ElasticsearchDataType.INTEGER,null,null));
        properties.put("age", ElasticsearchDataType.getType(ElasticsearchDataType.INTEGER,null,null));
        properties.put("id", ElasticsearchDataType.getType(ElasticsearchDataType.TEXT,null,null));
        properties.put("deviceNo_text", ElasticsearchDataType.getType(ElasticsearchDataType.TEXT,null,null));
        properties.put("deviceNo_keyword", ElasticsearchDataType.getType(ElasticsearchDataType.KEYWORD,null,null));
        properties.put("birth_date", ElasticsearchDataType.getType(ElasticsearchDataType.DATE,null,null));
        return elasticsearchUtil.putMapping(createIndexReq, properties);
    }

    @PostMapping("/getMapping")
    public Object getMapping(@RequestBody CreateIndexReq createIndexReq) throws Exception {
        return elasticsearchUtil.getMapping(createIndexReq);
    }

    @PostMapping("/indexAliases")
    public Object indexAliases(@RequestBody CreateIndexReq createIndexReq) throws Exception {
        return elasticsearchUtil.indexAliases(createIndexReq);
    }

    @PostMapping("/createIndexExists")
    public Object createIndexExists(@RequestBody CreateIndexReq createIndexReq) throws Exception {
        return elasticsearchUtil.createIndexExists(createIndexReq.getIndex());
    }

    @PostMapping("/indexDelete")
    public Object indexDelete(@RequestBody IndexDeleteReq indexDeleteReq) throws Exception {
        return elasticsearchUtil.indexDelete(indexDeleteReq);
    }
}
